import { generateId } from "@/utils/index";
import { typeList } from "@/utils/local";
import {
    mapParameterValues,
    obtainStreamFunctionsFromResponse,
} from "@/views/edge/windowsPage/js/index";
import { mapGetters } from "vuex";
export const myMixin = {
    data() {
        return {
            streamHandlerChecked: false,
            name: "",
            streamHandlerList: [],
            typeList: typeList,
            rowItem: {},
            rowId: "",
            objKey: "",
        };
    },
    computed: {
        ...mapGetters(["metadata"]),
    },
    methods: {
        // 下拉框和弹出框选中赋值给输入框
        changeRadio(row, item, index) {
            item.value = row.valueType;
        },
        updateList(data) {
            this.streamHandlerList = data;
        },
        handDel(row, index) {
            this.streamHandlerList.splice(index, 1);
        },
        changeType(index, row) {
            // 更换类型,参数置空
            this.$set(row, "showItem", false);
            row.showItem = false;
            row.title = this.typeList.find((item) => item.id == index)?.name || "";
            // 筛选 的value是个字符串-其他的是一个对象
            if (row.title == "筛选") {
                row.value = "";
            } else {
                row.value = {
                    function: "",
                    parameters: {
                        functionName: "",
                        parameters: [],
                    },
                };
            }
            this.$emit("changeType", index, row);
        },
        // 删除
        handItemDel(row, index) {
            row.value.splice(index, 1);
            this.$emit("handItemDel", index);
        },
        // 带...的 动态添加属性
        handItemAdd(row) {
            // const newRow = this.streamHandlerList.find((item) => item.id == row.id);
            row.value.push({
                defaultValue: "",
                description: "",
                isDynamic: true,
                name: "",
                optional: true,
                isDel: true,
                id: generateId(),
                value: "",
            });
        },
        submit(row) {
            this.streamHandlerList.forEach((item) => {
                if (item.id === this.rowId) {
                    // 窗口的和其他的走不同的key
                    const key =
                        item.type == "WINDOW" ? "windowFunctionNames" : "streamFunctions";
                    item.value.function = row.name || row.value;
                    item.value.parameters = mapParameterValues(
                        {
                            function: row.name || row.value,
                            parameters: [],
                        },
                        obtainStreamFunctionsFromResponse(this.metadata)[key]
                    );
                }
            });
        },
        handInput(item) {
            // 三种类型的抽屉数组
            this.rowItem = item;
            switch (item.title) {
                case "功能函数":
                    this.objKey = "arr";
                    break;
                case "筛选":
                    this.objKey = "funArr";
                    break;
                case "窗口":
                    this.objKey = "windowArr";
                    break;
                default:
                    this.objKey = "arr";
                    break;
            }
            this.rowId = item.id;
        },
        setType(type) {
            var newType = "";
            switch (type) {
                case "功能函数":
                    newType = "FUNCTION";
                    break;
                case "筛选":
                    newType = "FILTER";
                    break;
                case "窗口":
                    newType = "WINDOW";
                    break;
                default:
                    break;
            }
            return newType;
        },
        handOpenItem(item) {
            if (item.title === "筛选" || item.value.parameters.parameters.length == 0)
                return;
            this.$set(item, "showItem", !item.showItem);
            // item.showItem = !item.showItem;
        },
        getFlowData() {
            // 流处理数据获取
            // 选中了在返回,否则返回空数组
            return {
                streamHandlerList: this.streamHandlerChecked
                    ? this.setFlow(this.streamHandlerList)
                    : [],
                streamHandlerChecked: this.streamHandlerChecked,
            };
        },
        setFlow(data) {
            const newData = _.cloneDeep(data);
            let isMultiValues = [];

            newData.forEach((item) => {
                if (item.title !== "筛选") {
                    if (!item.value.function) {
                        this.$msg(item.title + "不能为空");
                        throw new Error(item.title + "不能为空");
                    }
                    item.value.parameters = item.value.parameters.parameters.map((v) => {
                        // 判断是否是多值
                        if (v.isMultiValue) {
                            isMultiValues = [];
                            isMultiValues = v.value.map((e) => {
                                if (!e.value) {
                                    this.$msg(v.name + "不能为空");
                                    throw new Error(v.name + "不能为空");
                                }
                                return e.value;
                            });
                        } else {
                            isMultiValues = [];
                            if (!v.value) {
                                this.$msg(v.name + "不能为空");
                                throw new Error(v.name + "不能为空");
                            }
                            return v.value;
                        }
                    });
                    item.value.parameters = item.value.parameters
                        .concat(isMultiValues)
                        .filter((v) => v);
                } else {
                    if (!item.value) {
                        this.$msg(item.title + "不能为空");
                        throw new Error(item.name + "不能为空");
                    }
                }
            });
            return newData;
        },
        delAttribute() { },
        //函数-窗口..
        handAdd(row) {
            // type 添加的类型

            const value =
                row.id == "FILTER"
                    ? ""
                    : {
                        function: "",
                        parameters: {
                            functionName: "",
                            parameters: [],
                        },
                    };
            this.streamHandlerList.push({
                title: row.name,
                type: row.id,
                id: generateId(),
                showItem: false, //子节点是否打开
                value,
                // parameters: [], //这里展开的内容
            });
            this.$emit("handAdd", row);
        },
    },
};
